package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.ProductInventory;
import com.example.mall.domain.ProductPromotions;
import com.example.mall.dto.ProductInventoryDto;
import com.example.mall.dto.ProductPromotionsDto;
import com.example.mall.mapper.ProductPromotionsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductInventoryQuery;
import com.example.mall.query.ProductPromotionsQuery;
import com.example.mall.service.IProductPromotionsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品促销活动表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class ProductPromotionsServiceImpl extends ServiceImpl<ProductPromotionsMapper, ProductPromotions> implements IProductPromotionsService {

    @Autowired
    private ProductPromotionsMapper productPromotionsMapper;

    @Override
    public PageDTO<ProductPromotionsDto> queryPage(ProductPromotionsQuery productPromotionsQuery) {
        if (productPromotionsQuery == null) {
            productPromotionsQuery = new ProductPromotionsQuery();
        }
        Page<ProductPromotions> page = productPromotionsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<ProductPromotions> cartPage = lambdaQuery()
                .like(productPromotionsQuery.getProductId()!= null, ProductPromotions::getProductId, productPromotionsQuery.getProductId())
                .like(productPromotionsQuery.getPromotionId()!= null, ProductPromotions::getPromotionId, productPromotionsQuery.getPromotionId())
                .like(productPromotionsQuery.getPromotionName()!= null, ProductPromotions::getPromotionName, productPromotionsQuery.getPromotionName())
                .like(productPromotionsQuery.getStatus()!= null, ProductPromotions::getStatus, productPromotionsQuery.getStatus())
                .apply(productPromotionsQuery.getStartTime() != null,
                        "DATE_FORMAT(start_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        productPromotionsQuery.getStartTime())
                .apply(productPromotionsQuery.getEndTime() != null,
                        "DATE_FORMAT(end_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        productPromotionsQuery.getEndTime())
                .orderByDesc(ProductPromotions::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, ProductPromotionsDto.class);
    }

    @Override
    public ProductPromotions queryProductPromotionsById(Long promotionId) {
        AssertUtil.isNotNull(promotionId, "promotionId不能为空");
        return productPromotionsMapper.selectById(promotionId);
    }

    @Override
    public List<ProductPromotions> queryProductPromotionsList() {
        return this.list();
    }

    @Override
    public void insert(ProductPromotionsDto productPromotionsDto) {
        ProductPromotions productPromotions = new ProductPromotions();
        CopyUtils.copyProperties(productPromotionsDto, productPromotions);
        productPromotions.setPromotionId(CodeGenerateUtils.generateUniqueId());
        productPromotions.setStatus(BaseConstants.NOT_USED);
        productPromotions.setCreateDate(new Date());
        productPromotions.setUpdateDate(new Date());
        productPromotionsMapper.insert(productPromotions);
    }

    @Override
    public void update(ProductPromotionsDto productPromotionsDto) {
        AssertUtil.isNotNull(productPromotionsDto.getPromotionId(), "promotionId不能为空");
        ProductPromotions productPromotions = productPromotionsMapper.selectById(productPromotionsDto.getPromotionId());
        AssertUtil.isNotNull(productPromotions, "商品促销活动不存在");
        CopyUtils.copyProperties(productPromotionsDto, productPromotions);
        productPromotions.setUpdateDate(new Date());
        productPromotionsMapper.updateById(productPromotions);
    }

    @Override
    public void delete(Long promotionId) {
        AssertUtil.isNotNull(promotionId, "promotionId不能为空");
        ProductPromotions productPromotions = productPromotionsMapper.selectById(promotionId);
        AssertUtil.isNotNull(productPromotions, "商品促销活动不存在");
        productPromotions.setStatus(BaseConstants.EXPIRED);
        productPromotions.setUpdateDate(new Date());
        productPromotionsMapper.updateById(productPromotions);
    }

    @Override
    public void deleteBatch(List<Long> promotionIds) {
        AssertUtil.isNotNull(promotionIds, "promotionIds不能为空");
        List<ProductPromotions> productPromotions = productPromotionsMapper.selectBatchIds(promotionIds);
        AssertUtil.isNotNull(productPromotions, "商品促销活动不存在");
        for (ProductPromotions productPromotion : productPromotions) {
            productPromotion.setStatus(BaseConstants.EXPIRED);
            productPromotion.setUpdateDate(new Date());
            productPromotionsMapper.updateById(productPromotion);
        }
    }
}
